<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\Session;
/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        //总条件：当前登陆身份
        $adminId = Session::get("admin")["id"];
        $hid = Session::get("admin")["hotelid"];


        $todayStart= date('Y-m-d 00:00:00', time()); //date
        $todayEnd= date('Y-m-d 23:59:59', time()); //data
        $todayS=strtotime($todayStart); //int
        $todayE= strtotime($todayEnd);//int

        $userdb = db("user");
        //总用户
        $totaluser = $userdb->where(["from"=>1])->count("id");
        //日增用户
        $dayuser = $userdb->where(["from"=>1])->where("ctime>='$todayStart' and ctime <= '$todayEnd'")->count("id");

        $oderdb = db("order");

        if($adminId == 1){
            //总订单数量 统计的是 订单状态 完成 退款状态 完成
            $totalorder = $oderdb->where(["pay_status"=>3,"refund_status"=>3])->count("id");
            //日增订单
            $dayorder = $oderdb->where("pay_status in (1,2) and add_time >= $todayS and add_time <= $todayE")->count("id");

            //总金额 计算订单收益 只有完结的订单才计算收益 前台用户结算后计算收益或者后台管理员操作后计算收益 收益：去掉押金后最终收益
            $totalmoney = $oderdb->where(["pay_status"=>3,"refund_status"=>3])->sum("income");
            $totalmoney2 =  $oderdb->where(["pay_status"=>4])->sum("income");

            $totalmoney += $totalmoney2;

            //今日金额
            $daymoney = $oderdb->where(["pay_status"=>3,"refund_status"=>3])->where("add_time >= $todayS and add_time <= $todayE")->sum("income");

            //酒店数量
            $hotels = db("hotels")->where(["h_status"=>"normal"])->count();
            //房源数量
            $hours = db("house")->where(["h_status"=>"normal"])->count();

        }else{
            //总订单数量 统计的是 订单状态 完成 退款状态 完成
            $totalorder = $oderdb->where(["pay_status"=>3,"refund_status"=>3])->where("hotelid" ,'in',$hid)->count("id");

            //日增订单
            $dayorder = $oderdb->where("pay_status in (1,2) and add_time >= $todayS and add_time <= $todayE")->where("hotelid" ,'in',$hid)->count("id");

            //总金额 计算订单收益 只有完结的订单才计算收益 前台用户结算后计算收益或者后台管理员操作后计算收益 收益：去掉押金后最终收益
            $totalmoney = $oderdb->where(["pay_status"=>3,"refund_status"=>3])->where("hotelid" ,'in',$hid)->sum("income");
            //计算取消支付订单收益
            $totalmoney2 =  $oderdb->where(["pay_status"=>4])->where("hotelid" ,'in',$hid)->sum("income");

            $totalmoney += $totalmoney2;

            //今日金额
            $daymoney = $oderdb->where(["pay_status"=>3,"refund_status"=>3])->where("add_time >= $todayS and add_time <= $todayE")->where("hotelid" ,'in',$hid)->sum("income");

            //酒店数量
            $hotels = db("hotels")->where(["h_status"=>"normal"])->where("hid" ,'in',$hid)->count();

            //房源数量
            $hours = db("house")->where(["h_status"=>"normal"])->where("h_id" ,'in',$hid)->count();

        }





        $this->view->assign([
            'adminId' =>$adminId,
            'totaluser'        => $totaluser,
            'dayuser'        => $dayuser,
            'totalorder'        => $totalorder,
            'dayorder'        => $dayorder,
            'totalmoney'        => $totalmoney,
            'daymoney'        => $daymoney,
            'hotels'        => $hotels,
            'hours'        => $hours
        ]);

        return $this->view->fetch();
    }

}
